/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var interval;

function chatwith(user1,user2){
    var req = makeObjRequest();
    req.onreadystatechange = function(){
        if (req.readyState==4 && req.status==200)
        {
            chatroom(user1, user2)
        }
    }
    req.open("GET","ProcessingCommand?value1=chat&value2="+user2+"&value3=");
    req.send();
}

function chatroom(user1,user2){
    var title = document.getElementById('tabs');
    var blocktitle = document.createElement('div')
    blocktitle.setAttribute('class', 'titlechatorroom')
    blocktitle.id = user1+user2;
    var link = document.createElement('a')
    link.setAttribute('href', 'javascript:void()')
    link.setAttribute('onclick', 'showchat(\''+blocktitle.id+'content\',\'form'+ blocktitle.id+'\')')
    link.innerHTML = user2;
    blocktitle.appendChild(link);
    if(document.getElementById(blocktitle.id) == null)
        title.appendChild(blocktitle);
    var blockchat = document.getElementById('contentschatbox')
    var chatroom = document.createElement('div')
    chatroom.setAttribute('class', 'chatroom')
    chatroom.setAttribute('name', 'hide')
    chatroom.id = user1+user2+'content';
    chatroom.setAttribute('style', 'display:none')
    //clearInterval(interval);
    //setInterval(function(){getchatcontent(chatroom.id, user1, user2)}, 500) ;
    getchatcontent(chatroom.id, user1, user2)
    if(document.getElementById(chatroom.id)== null)
        blockchat.appendChild(chatroom)
    var chatboxcommand = document.getElementById('chatboxcommands');
    var newform = document.createElement('form')
    newform.id = 'form'+blocktitle.id
    newform.setAttribute('class', 'formchatbox')
    newform.setAttribute('name', 'commandform')
    newform.setAttribute('method', 'post')
    newform.setAttribute('action', 'javascript:parsecommand(\''+chatroom.id+'\',\'input_'+user1+'_'+user2+'\');')
    newform.setAttribute('style', 'display:none')
    var input = document.createElement('input')
    input.setAttribute('type', 'text')
    input.setAttribute('name', 'inputchatboxtext')
    input.setAttribute('id', 'input_'+user1+'_'+user2)
    newform.appendChild(input)
    chatboxcommand.appendChild(newform)
//<form id="formhome" class ="formchatbox" name="commandform" method="post" action="javascript:parsecommand('home','inputchatboxtext');">
//                   <input type="text" name="inputchatboxtext" id="inputchatboxtext"/>
//              </form>
}

function showchat(id,idinput){
    var temp = document.getElementById(id)
    var tempform = document.getElementById(idinput)
    var allchatroom = document.getElementsByName("hide")
    for(var i=0; i< allchatroom.length;++i){
        allchatroom[i].style.display = 'none';
    }
    var allcommandform = document.getElementsByName("commandform")
    for(var j=0; j< allcommandform.length;++j){
        allcommandform[j].style.display = 'none';
    }
    //if(temp.style.display == 'none' )
    temp.style.display = 'block'
    tempform.style.display = 'block';
//else
//    temp.style.display = 'none'
}

function getchatcontent(id, user1, user2){
    var req = makeObjRequest();
    req.onreadystatechange = function(){
        if (req.readyState==4 && req.status==200)
        {
            document.getElementById(id).innerHTML= req.responseText;
            setTimeout(function(){
                getchatcontent(id,user1,user2)
                }, 1123);
        }
    }

    req.open("GET","ContentChatHandler?value="+user1+"&value2="+user2, true);
    req.send();

}

function getNamefromID(id){
    var req = makeObjRequest();
    req.onreadystatechange = function(){
        if (req.readyState==4 && req.status==200)
        {
            var name = req.responseText;
            alert (name);
        }
        return name;
    }
    req.open("GET","Name?categoryid="+id, true);
    req.send();
}

function expandTree(id){
    var req = makeObjRequest();
    req.onreadystatechange = function(){
        if (req.readyState==4 && req.status==200)
        {
            var array = req.responseText.split("iniadalahpemisah");
            var categoryid = "categorytree"+id;
            var object = document.getElementById(categoryid);
            var output = "";
            if(object.innerHTML == ""){
                for(i=0;i<array.length-1;i=i+2) {
                    output += "<li>"
                    output += "<a href ='javascript:void();' onclick = 'javascript:expandTree("+array[i]+")'>"+array[i+1]+"</a>";
                    output += "<ul id = 'categorytree"+array[i]+"'></ul>";
                    output += "</li>"
                }
                object.innerHTML = output;
            }else{
                object.innerHTML = "";
            }
        }
    }
    req.open("GET","Child?categoryid="+id, true);
    req.send();
}
